﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;


public partial class SiteMaster : System.Web.UI.MasterPage
{
    public string navMenu = string.Empty;

    private DataTable GetUserDataFromSession()
    {
        DataTable result = null;

        if (Session["UserData"] != null)
        {
            result = (DataTable)Session["UserData"];
        }       

        return result;
    }

    protected void Page_Load(object sender, EventArgs e)
    {   
        navMenu = GenerateNavigationMenu();  
    }
    
    public string GenerateNavigationMenu()
    {
        //sidebar.Attributes.Add("visible", "show");
        string resultMenu = string.Empty;       
        //todo: get user role
        
        int roleType = 0;
        
        DataTable roleTable = GetUserDataFromSession(); 
        if (roleTable != null && roleTable.Rows.Count == 1)
        {            
            int.TryParse(roleTable.Rows[0]["roletype"].ToString(), out roleType);
        }
        resultMenu = StartNavigationMenu();

        string selectedMenu = Request.RawUrl;

        switch (roleType)
        {
            case 0: //guest

                if (selectedMenu.Equals("/PROGRAMARE/Home.aspx"))                   resultMenu = FillNavigationMenu(resultMenu, "Home.aspx", "Acasă","Prima pagina", true);
                else                                                                resultMenu = FillNavigationMenu(resultMenu, "Home.aspx", "Acasă","Prima pagina", false);

                if (selectedMenu.Equals("/PROGRAMARE/About.aspx"))                  resultMenu = FillNavigationMenu(resultMenu, "About.aspx", "Detalii","Detalii", true);
                else                                                                resultMenu = FillNavigationMenu(resultMenu, "About.aspx", "Detalii","Detalii", false);

                if (selectedMenu.Equals("/PROGRAMARE/faq.aspx"))                    resultMenu = FillNavigationMenu(resultMenu, "faq.aspx", "FAQ", "Intreaba", true);
                else                                                                resultMenu = FillNavigationMenu(resultMenu, "faq.aspx", "FAQ", "Intreaba", false);

                if (selectedMenu.Equals("/PROGRAMARE/Misiuni.aspx"))                resultMenu = FillNavigationMenu(resultMenu, "Misiuni.aspx", "Misiuni","Misiuni", true);
                else                                                                resultMenu = FillNavigationMenu(resultMenu, "Misiuni.aspx", "Misiuni","Misiuni", false);

                if (selectedMenu.Equals("/PROGRAMARE/Programare.aspx"))             resultMenu = FillNavigationMenu(resultMenu, "Programare.aspx", "Programare","Rezerveaza", true);
                else                                                                resultMenu = FillNavigationMenu(resultMenu, "Programare.aspx", "Programare","Rezerveaza", false);

                if (selectedMenu.Equals("/PROGRAMARE/Default.aspx?area=regForm"))   resultMenu = FillNavigationMenu(resultMenu, "Default.aspx?area=regForm", "Înregistrare","Autentificare", true);
                else                                                                resultMenu = FillNavigationMenu(resultMenu, "Default.aspx?area=regForm", "Înregistrare","Autentificare", false);

                break;

            case 1: //BackEnd Super Administrator
                if (selectedMenu.Equals("/PROGRAMARE/Classifiers.aspx?area=classifierTypes")) resultMenu = FillNavigationMenu(resultMenu, "Classifiers.aspx?area=classifierTypes", "Clasificator tip","Lista de clasificatoare", true);
                else                                                                        resultMenu = FillNavigationMenu(resultMenu, "Classifiers.aspx?area=classifierTypes", "Clasificator tip","Lista de clasificatoare", false);

                if (selectedMenu.Equals("/PROGRAMARE/Classifiers.aspx?area=classifiers"))   resultMenu = FillNavigationMenu(resultMenu, "Classifiers.aspx?area=classifiers", "Clasificatoare","Clasificatoare", true);
                else                                                                        resultMenu = FillNavigationMenu(resultMenu, "Classifiers.aspx?area=classifiers", "Clasificatoare","Clasificatoare", false);

                if (selectedMenu.Equals("/PROGRAMARE/Users.aspx?area=users"))               resultMenu = FillNavigationMenu(resultMenu, "Users.aspx?area=users", "Users","Utilizatori", true);
                else                                                                        resultMenu = FillNavigationMenu(resultMenu, "Users.aspx?area=users", "Users","Utilizatori", false);

                if (selectedMenu.Equals("/PROGRAMARE/Consulate.aspx?area=consulatList"))    resultMenu = FillNavigationMenu(resultMenu, "Consulate.aspx?area=consulatList", "Consulat List","Lista de consulate", true);
                else                                                                        resultMenu = FillNavigationMenu(resultMenu, "Consulate.aspx?area=consulatList", "Consulat List","Lista de consulate", false);

                if (selectedMenu.Equals("/PROGRAMARE/Services.aspx?area=servicesList"))     resultMenu = FillNavigationMenu(resultMenu, "Services.aspx?area=servicesList", "Services List","Lista de servicii", true);
                else                                                                        resultMenu = FillNavigationMenu(resultMenu, "Services.aspx?area=servicesList", "Services List","Lista de servicii", false);

                if (selectedMenu.Equals("/PROGRAMARE/AdminPage.aspx?area=templatezi"))      resultMenu = FillNavigationMenu(resultMenu, "AdminPage.aspx?area=templatezi", "Templatezi","Template day", true);
                else                                                                        resultMenu = FillNavigationMenu(resultMenu, "AdminPage.aspx?area=templatezi", "Templatezi","Template day", false);

                if (selectedMenu.Equals("/PROGRAMARE/AdminPage.aspx?area=calendar"))        resultMenu = FillNavigationMenu(resultMenu, "AdminPage.aspx?area=calendar", "Calendar","Calendar", true);
                else                                                                        resultMenu = FillNavigationMenu(resultMenu, "AdminPage.aspx?area=calendar", "Calendar","Calendar", false);

                if (selectedMenu.Equals("/PROGRAMARE/AdminPage.aspx?area=sarbatori"))       resultMenu = FillNavigationMenu(resultMenu, "AdminPage.aspx?area=sarbatori", "Sarbatori","Sarbatori", true);
                else                                                                        resultMenu = FillNavigationMenu(resultMenu, "AdminPage.aspx?area=sarbatori", "Sarbatori","Sarbatori", false);

                if (selectedMenu.Equals("/PROGRAMARE/AdminPage.aspx?area=programari"))      resultMenu = FillNavigationMenu(resultMenu, "AdminPage.aspx?area=programari", "Programari", "Sumarul Programarilor", true);
                else                                                                        resultMenu = FillNavigationMenu(resultMenu, "AdminPage.aspx?area=programari", "Programari", "Sumarul Programarilor", false);


                break;

            case 2: //BackEnd Time Administrator

                if (selectedMenu.Equals("/PROGRAMARE/AdminPage.aspx?area=templatezi"))      resultMenu = FillNavigationMenu(resultMenu, "AdminPage.aspx?area=templatezi", "Templatezi","Template day", true);
                else                                                                        resultMenu = FillNavigationMenu(resultMenu, "AdminPage.aspx?area=templatezi", "Templatezi","Template day", false);

                if (selectedMenu.Equals("/PROGRAMARE/AdminPage.aspx?area=calendar"))        resultMenu = FillNavigationMenu(resultMenu, "AdminPage.aspx?area=calendar", "Calendar","Calendar", true);
                else                                                                        resultMenu = FillNavigationMenu(resultMenu, "AdminPage.aspx?area=calendar", "Calendar","Calendar", false);

                if (selectedMenu.Equals("/PROGRAMARE/AdminPage.aspx?area=sarbatori"))       resultMenu = FillNavigationMenu(resultMenu, "AdminPage.aspx?area=sarbatori", "Sarbatori","Sarbatori", true);
                else                                                                        resultMenu = FillNavigationMenu(resultMenu, "AdminPage.aspx?area=sarbatori", "Sarbatori","Sarbatori", false);

                if (selectedMenu.Equals("/PROGRAMARE/AdminPage.aspx?area=programari"))      resultMenu = FillNavigationMenu(resultMenu, "AdminPage.aspx?area=programari", "Programari", "Sumarul Programarilor", true);
                else                                                                        resultMenu = FillNavigationMenu(resultMenu, "AdminPage.aspx?area=programari", "Programari", "Sumarul Programarilor", false);

                break;

            case -1:    //FrontEnd

                if (selectedMenu.Equals("/PROGRAMARE/Default.aspx?area=info"))          resultMenu = FillNavigationMenu(resultMenu, "Default.aspx?area=info", "Acasă","Prima pajina", true);
                else                                                                    resultMenu = FillNavigationMenu(resultMenu, "Default.aspx?area=info", "Acasă","Prima pajina", false);


                if (selectedMenu.Equals("/PROGRAMARE/Default.aspx?area=about"))         resultMenu = FillNavigationMenu(resultMenu, "Default.aspx?area=about", "Detalii","Detalii", true);
                else                                                                    resultMenu = FillNavigationMenu(resultMenu, "Default.aspx?area=about", "Detalii","Detalii", false);

                if (selectedMenu.Equals("/PROGRAMARE/Default.aspx?area=orgForm"))       resultMenu = FillNavigationMenu(resultMenu, "Default.aspx?area=orgForm", "Programare","Programare", true);
                else                                                                    resultMenu = FillNavigationMenu(resultMenu, "Default.aspx?area=orgForm", "Programare","Programare", false);

                if (selectedMenu.Equals("/PROGRAMARE/faq.aspx"))                        resultMenu = FillNavigationMenu(resultMenu, "faq.aspx", "FAQ","Raspuns", true);
                else                                                                    resultMenu = FillNavigationMenu(resultMenu, "faq.aspx", "FAQ","Raspuns", false);

                if (selectedMenu.Equals("/PROGRAMARE/Misiuni.aspx"))                    resultMenu = FillNavigationMenu(resultMenu, "Misiuni.aspx", "Misiuni", "Misiuni", true);
                else                                                                    resultMenu = FillNavigationMenu(resultMenu, "Misiuni.aspx", "Misiuni", "Misiuni", false);

                break;

            default:
                break;
        }

        return resultMenu;
       
    }

    private string FillNavigationMenu(string resultMenu, string navURL, string nameMenu, string metaMenu, bool isSelected)
    {
        resultMenu += "<li>";
        if (isSelected)
        {
            resultMenu += "<a href=\"" + navURL + "\"  class=\"current\">";
        }
        else
        {
            resultMenu += "<a href=\"" + navURL + "\">";
        }
        resultMenu += nameMenu+"</a></li>";
        //resultMenu += "<span class=\"meta\">" + metaMenu + "</span><br />" + nameMenu + "</a></li>";
        return resultMenu;
    }

    private string StartNavigationMenu()
    {
        return string.Format("<ul id=\"navigation\" >");
    }

    private string EndNavigationMenu(string navMenu)
    {
        return navMenu+string.Format("<!--/ul-->");
    }
    
    protected void LogInLogOutLinkButton_Load(object sender, EventArgs e)
    {
        HyperLink link = (HyperLink)this.FindControl("LogInLogOutLinkButton");

        DataTable roleTable = GetUserDataFromSession();
        if (roleTable != null && roleTable.Rows.Count == 1)
        {
            string firstName = roleTable.Rows[0]["Nume"].ToString();
            string lastName = roleTable.Rows[0]["Prenume"].ToString();
            link.Text = firstName + " " + lastName + " | Log OUT";
            link.NavigateUrl = "~/AdminPage.aspx?action=logout";
            link.ImageUrl = "";
        }
        else
        {
            link.Text = "Log IN";
            link.NavigateUrl = "~/LogInPage.aspx";
        }
    }
}
